/*
 * @Description:
 * @Version: 2.0
 * @Autor: Seven
 * @Date: 2023-10-19 16:08:00
 * @LastEditors: Seven
 * @LastEditTime: 2023-12-11 15:51:05
 */
import { getKnowledgeAdd, getKnowledgeEdit } from "@/services/modules/plan";
import { Form, Input, Modal, Select, Upload, Button, message } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import React, { memo, useEffect, useRef } from "react";
import { loacalCache } from "@/ultils/func";

const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 18 },
  },
};
const { Option } = Select;
const AddModal = memo(
  ({ modalVisible, handleCancel, getList, title = "新增", rowInfoData }) => {
    const formRef = useRef();
    const [form] = Form.useForm();
    function onFinish() {
      form
        .validateFields({ validateOnly: true })
        .then(() => {
          if (title === "编辑") {
            let data = {
              ...form.getFieldsValue(),
            };
            getKnowledgeEdit(rowInfoData.id, data).then((res) => {
              if (res.code === 200) {
                getList();
                handleCancel();
              }
            });
          } else {
            let dataAdd = {
              ...form.getFieldsValue(),
              file: form.getFieldsValue().file[0]?.originFileObj,
            };
            console.log("dataAdd", dataAdd);
            getKnowledgeAdd(dataAdd).then((res) => {
              if (res.code === 200) {
                message.success(res?.msg);
                getList();
                handleCancel();
              }
            });
          }
        })
        .catch((err) => err);
    }

    useEffect(() => {
      if (title === "编辑") {
        form.setFieldsValue({
          ...rowInfoData,
        });
      }
    }, [rowInfoData, form, title]);
    const normFile = (e) => {
      const formData = new FormData();
      formData.append("file", e);
      if (Array.isArray(e)) {
        return e;
      }
      return e?.fileList;
    };
    return (
      <div>
        <Modal
          title={title}
          open={modalVisible}
          onOk={onFinish}
          onCancel={handleCancel}
        >
          <Form
            {...formItemLayout}
            ref={formRef}
            onFinish={onFinish}
            form={form}
            style={{
              maxWidth: 600,
            }}
          >
            {title !== "编辑" && (
              <Form.Item
                name="knowledgeId"
                label="编号"
                rules={[
                  {
                    required: true,
                    message: "请输入你的编号",
                  },
                ]}
              >
                <Input />
              </Form.Item>
            )}
            <Form.Item
              name="speciality"
              label="专业"
              rules={[
                {
                  required: true,
                  message: "请选择你的专业",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")?.["falut_speciality"] &&
                  loacalCache
                    .getCache("DicDataList")
                    ?.["falut_speciality"].map((item) => {
                      return (
                        <Option value={item.value} key={item.id}>
                          {item.label}
                        </Option>
                      );
                    })}
              </Select>
            </Form.Item>

            <Form.Item
              name="device"
              label="设备分类"
              rules={[
                {
                  required: true,
                  message: "请选择你的设备分类",
                },
              ]}
            >
              <Input />
            </Form.Item>
            {title !== "编辑" && (
              <Form.Item
                name="createBy"
                label="创建人"
                rules={[
                  {
                    required: true,
                    message: "请输入你的创建人",
                  },
                ]}
              >
                <Input />
              </Form.Item>
            )}

            <Form.Item
              name="description"
              label="描述"
              rules={[
                {
                  required: true,
                  message: "请输入你的描述",
                },
              ]}
            >
              <Input />
            </Form.Item>
            <Form.Item
              name="line"
              label="线路"
              rules={[
                {
                  required: true,
                  message: "请选择你的线路",
                },
              ]}
            >
              <Select>
                {loacalCache.getCache("DicDataList")?.["line"] &&
                  loacalCache.getCache("DicDataList")?.["line"].map((item) => {
                    return (
                      <Option value={item.value} key={item.id}>
                        {item.label}
                      </Option>
                    );
                  })}
              </Select>
            </Form.Item>
            {title !== "编辑" && (
              <Form.Item
                name="status"
                label="状态"
                rules={[
                  {
                    required: true,
                    message: "请输入你的状态",
                  },
                ]}
              >
                <Select>
                  {loacalCache.getCache("DicDataList")?.["knowledge_status"] &&
                    loacalCache
                      .getCache("DicDataList")
                      ?.["knowledge_status"].map((item) => {
                        return (
                          <Option value={item.value} key={item.id}>
                            {item.label}
                          </Option>
                        );
                      })}
                </Select>
              </Form.Item>
            )}
            {title !== "编辑" && (
              <Form.Item
                name="file"
                label="Upload"
                valuePropName="fileList"
                getValueFromEvent={normFile}
              >
                <Upload
                  name="file"
                  beforeUpload={() => false}
                  listType="picture"
                >
                  <Button icon={<UploadOutlined />}>上传图片</Button>
                </Upload>
              </Form.Item>
            )}
          </Form>
        </Modal>
      </div>
    );
  }
);

export default AddModal;
